package com.pc.web.goods;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

@Service
public class GoodsService {
	@Autowired GoodsDao dao;
	public void list(Model model){
		List<GoodsBeans> list=dao.list();//리스트를 배열로
		model.addAttribute("list",list);
	}
	
	public GoodsBeans insert(GoodsBeans bean, Map<String, Object> fileList){
		List<MultipartFile> file=(List<MultipartFile>) bean.getFile();
		
		for(int i=0; i<file.size(); i++){

			String originName = file.get(i).getOriginalFilename().replaceAll(" ", "_");
			if(file.get(i).getOriginalFilename().isEmpty()){
				switch(i){
				case 0: bean.setGoods_image1("");break;
				case 1: bean.setGoods_image2("");break;
				case 2: bean.setGoods_image3("");break;
				case 3: bean.setGoods_image4("");break;
				case 4: bean.setGoods_image5("");break;
				case 5: bean.setGoods_image6("");break;
				case 6: bean.setGoods_image7("");break;
				case 7: bean.setGoods_image8("");break;
				case 8: bean.setGoods_image9("");break;
				case 9: bean.setGoods_image10("");break;
				case 10: bean.setGoods_image11("");break;
				case 11: bean.setGoods_image12("");break;				
				}
			}
			else{
				String extend = originName.substring(originName.lastIndexOf(".") - 1,originName.length());
				String imageName =(i)+System.currentTimeMillis() + file.size()+ extend;
				String path = "C:/Program Files/Project/pcmaster/pcmaster/src/main/webapp/resources/upload";
	
				// 1375780497293플   				DB
				// 11375780416512플.jpg			서버
				
				fileList.put(imageName, imageName);	
				
				try {
					MultipartFile files = file.get(i);
					File f = new File(path + "/" + imageName);
					files.transferTo(f);
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				switch(i){
				case 0: bean.setGoods_image1(imageName);break;
				case 1: bean.setGoods_image2(imageName);break;
				case 2: bean.setGoods_image3(imageName);break;
				case 3: bean.setGoods_image4(imageName);break;
				case 4: bean.setGoods_image5(imageName);break;
				case 5: bean.setGoods_image6(imageName);break;
				case 6: bean.setGoods_image7(imageName);break;
				case 7: bean.setGoods_image8(imageName);break;
				case 8: bean.setGoods_image9(imageName);break;
				case 9: bean.setGoods_image10(imageName);break;
				case 10: bean.setGoods_image11(imageName);break;
				case 11: bean.setGoods_image12(imageName);break;	
				}
			}
		}
		return bean;
	}
}